package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.DesJson.Des3;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.utils.UIUtils;
import com.sczmgk.hotline_pzh.view.RegistItemView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/12 0012.
 */

public class RegistConfirmActivity extends BaseActivity {

    /*注册协议跳转时的请求码*/
    private static final int REQUEST_CODE_FOR_RESULT = 100;

    /*注册协议是否已读的控件*/
    private CheckBox mCbRead;

    /*注册协议的控件*/
    private TextView mTvProtocol;

    /*姓名,证件号,密码的条目控件*/
    private RegistItemView mConfirmName;
    private RegistItemView mConfirmCardId;
    private RegistItemView mConfirmPwd;

    /*注册协议的条目控件*/
    private LinearLayout mRegistShowProtocol;

    /*确认按钮*/
    private Button mBtnSure;

    /*输入的电话和验证码*/
    private String phoneInput;
    private String keyInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        //添加activity到集合中
        CommonUtils.addActivity(this);

        setHeader();

        initView();

        initData();

        setListener();

    }

    /**
     * 设置标题栏
     */
    private void setHeader() {
        setTitle(R.string.regist);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化确认界面的控件
     */
    private void initView() {

        //姓名
        mConfirmName = (RegistItemView) findViewById(R.id.riv_regist_phone);
        //证件号
        mConfirmCardId = (RegistItemView) findViewById(R.id.riv_regist_security_code);
        //密码
        mConfirmPwd = (RegistItemView) findViewById(R.id.riv_regist_pwd);

        //协议条目
        mRegistShowProtocol = (LinearLayout) findViewById(R.id.ll_show_protocol);
        //协议是否已读
        mCbRead = (CheckBox) findViewById(R.id.cb_confirm_read_protocol);
        //协议控件
        mTvProtocol = (TextView) findViewById(R.id.tv_confirm_protocol);

        //下一步
        mBtnSure = (Button) findViewById(R.id.btn_regist_next);

        //注册协议添加下划线
        mTvProtocol.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        mTvProtocol.getPaint().setAntiAlias(true);//抗锯齿

        //设置图片描述
        mConfirmName.setImageResource(R.drawable.account_number);
        mConfirmCardId.setImageResource(R.drawable.cardid);
        mConfirmPwd.setImageResource(R.drawable.password);

        //展示注册确定信息界面
        showConfirmActivity();
    }

    /**
     * 初始化确认界面的数据
     */
    private void initData() {

        //取得输入的电话和验证码
        Intent intent = getIntent();
        phoneInput = intent.getStringExtra(Constants.REGIST_PHONE_NUMBER);
        keyInput = intent.getStringExtra(Constants.REGIST_KEY);

    }

    /**
     * 设置监听
     */
    private void setListener() {

        //注册协议的点击事件
        mTvProtocol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到注册协议界面
                Intent intent = new Intent(RegistConfirmActivity.this, ProtocolActivity.class);
                startActivityForResult(intent, REQUEST_CODE_FOR_RESULT);
            }
        });

        //确认按钮的点击事件
        mBtnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果注册协议显示已读,就提交数据,否则弹吐司
                boolean isChecked = mCbRead.isChecked();
                if (isChecked) {
                    //注册提交
                    registSubmit();
                } else {
                    MyToast.show(RegistConfirmActivity.this, "未同意注册协议");
                }
            }
        });
    }

    /**
     * 注册提交
     */
    private void registSubmit() {

        //获取输入文本
        String name = mConfirmName.getText();
        String cardId = mConfirmCardId.getText();
        String password = mConfirmPwd.getText();

        if (Tools.isEmpty(name)) {
            MyToast.show(RegistConfirmActivity.this, "姓名不能为空");
            return;
        }

        if (Tools.isEmpty(cardId)) {
            MyToast.show(RegistConfirmActivity.this, "证件号不能为空");
            return;
        }

        //判断身份证号是否正确
        boolean isCorrect = Tools.checkUserId(cardId);
        if (!isCorrect) {
            MyToast.show(RegistConfirmActivity.this, "请输入正确的身份证号");
            return;
        }

        if (Tools.isEmpty(password)) {
            MyToast.show(RegistConfirmActivity.this, "密码不能为空");
            return;
        }

        //检查密码是否符合规范
        String correctToast = Tools.isCorrectPWD(password, phoneInput);
        if (!correctToast.equals("true")) {
            MyToast.show(RegistConfirmActivity.this, correctToast);
            return;
        }

        //提交数据
        submit(name, cardId, password);

    }

    /**
     * 数据都不为空,提交数据
     */
    private void submit(String name, String cardId, String password) {

        Map<String, Object> maps = new HashMap<>();
        maps.put("phone", phoneInput);
        maps.put("password", Des3.getEncodePWD(password));
        maps.put("consumersname", name);
        maps.put("nickname", phoneInput + "仁和");
        maps.put("idcard", cardId);
        maps.put("isagree", 0);
        MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_REGIST_URL, maps, MyHttp.POST, RegistConfirmActivity.this, false, null, new SimplePaserJsonCallback() {
            @Override
            public void onVerifyIsNull() {
                MyToast.showDebug(RegistConfirmActivity.this, "提交");
                //跳转到登录界面
                UIUtils.startActivity(RegistConfirmActivity.this, LoginActivity.class);
                //将注册界面都关掉
                CommonUtils.finishActivity();
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {

            }

            @Override
            public void onErrorCodeError(BaseEntity.MyError mError) {
                super.onErrorCodeError(mError);
                DialogFactory.warningDialog(RegistConfirmActivity.this,mError.msg);
            }
        });

    }

    /**
     * 展示注册确定信息界面
     */
    private void showConfirmActivity() {

        //设置展示协议的显示
        mRegistShowProtocol.setVisibility(View.VISIBLE);

        //设置第一个条目的显示与数据
        setItemData(mConfirmName, View.VISIBLE, R.string.personal_info_name, R.string.input_name, View.GONE, View.GONE, InputType.TYPE_CLASS_TEXT);
        //设置第二个条目的显示与数据
        setItemData(mConfirmCardId, View.VISIBLE, R.string.card_id_number, R.string.input_card_id, View.GONE, View.GONE, InputType.TYPE_CLASS_TEXT);
        //设置第三个条目的显示与数据
        setItemData(mConfirmPwd, View.VISIBLE, R.string.login_password, R.string.input_pwd, View.GONE, View.VISIBLE, InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        //设置按钮的文本
        mBtnSure.setText(R.string.sure);
    }

    /**
     * 设置条目控件的显示和文本展示
     *
     * @param riv            控件名
     * @param itemVisiable   条目是否显示
     * @param textDes        描述文本
     * @param hintDes        hint展示
     * @param getKeyVisiable 获取验证码的按钮是否显示
     * @param eyesVisiable   显示密码的按钮是否显示
     */
    private void setItemData(RegistItemView riv, int itemVisiable, int textDes, int hintDes, int getKeyVisiable, int eyesVisiable, int inputType) {

        //设置条目的显示
        riv.setVisibility(itemVisiable);
        //设置文本描述
        riv.setTextDes(textDes);
        //设置hint
        riv.setHint(hintDes);
        //设置获取验证码的显示
        riv.setGetKeyVisiable(getKeyVisiable);
        //设置是否显示眼睛的显示
        riv.setEyesVisiable(eyesVisiable);
        //设置输入类型
        riv.setInputType(inputType);
    }


    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
        super.onActivityResult(requestCode, resultCode, arg2);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_FOR_RESULT:
                    //如果是没有选择,则选择
                    if (!mCbRead.isChecked()) {
                        mCbRead.setChecked(true);
                    }
                    break;
            }
        }
    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }
}
